const router = require("koa-router")();
const { query } = require("./../config/dbPool");
const {
  ELECTIVELIST_SQL,
  ELECTIVEGRADE_SQL,
  ELECTIVEINFO_SQL,
  ELECTIVEADD_SQL,
  ELECTIVEUPDATE_SQL,
  ELECTIVEDELETE_SQL,
  COURSESPARETIME_SQL,
  COURSEINFO_SQL,
  COURSEUPDATE_SQL,
  ELECTIVEUSERLIST_SQL,
} = require("./../config/dbSQL");

router.prefix("/elective");

/**
 * 获取选课列表
 * @url /elective/list
 */
router.post("/list", async (ctx) => {
  const request = ctx.request.body;
  const { grade, course_id } = request;
  let res = JSON.parse(JSON.stringify(await query(ELECTIVELIST_SQL)));
  if (grade == "1" && course_id) {
    res.data = res.data.filter(
      (item) => !item.grade && item.course_id == course_id
    );
  } else if (grade == "1") {
    res.data = res.data.filter((item) => !item.grade);
  } else if (course_id) {
    res.data = res.data.filter((item) => item.course_id == course_id);
  }
  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      data: res.data,
      message: "获取成功",
    };
  } else {
    ctx.body = {
      status: 200,
      result: false,
      message: "获取失败",
    };
  }
});

/**
 * 根据token获取选课列表
 * @url /elective/user/list
 */
router.post("/user/list", async (ctx) => {
  const request = ctx.request.body;
  const user_id = ctx.request.header.token;
  const { grade, course_id } = request;
  let res = JSON.parse(
    JSON.stringify(await query(ELECTIVEUSERLIST_SQL, [user_id]))
  );
  if (grade == "1" && course_id) {
    res.data = res.data.filter(
      (item) => !item.grade && item.course_id == course_id
    );
  } else if (grade == "1") {
    res.data = res.data.filter((item) => !item.grade);
  } else if (course_id) {
    res.data = res.data.filter((item) => item.course_id == course_id);
  }

  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      data: res.data,
      message: "获取成功",
    };
  } else {
    ctx.body = {
      status: 200,
      result: false,
      message: "获取失败",
    };
  }
});

/**
 * 获取选课详情
 * @url /elective/:user_id/:course_id/info
 */
router.get("/:user_id/:course_id/info", async (ctx) => {
  const params = ctx.params;
  const { user_id, course_id } = params;
  const res = JSON.parse(
    JSON.stringify(await query(ELECTIVEINFO_SQL, [user_id, course_id]))
  );
  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      data: {
        ...res.data[0],
      },
      message: "获取成功",
    };
  } else {
    ctx.body = {
      status: 200,
      result: false,
      message: "获取失败",
    };
  }
});

/**
 * 添加选课
 * @url /elective/:course_id/info
 */
router.post(`/:course_id/info`, async (ctx) => {
  const params = ctx.params;
  const user_id = ctx.request.header.token;
  const { course_id } = params;
  const data = JSON.parse(
    JSON.stringify(await query(ELECTIVEINFO_SQL, [user_id, course_id]))
  );
  const queryData = {
    user_id,
    course_id,
  };
  if (data.result && data.data.length !== 0) {
    ctx.body = {
      status: 200,
      result: false,
      message: "已选课",
    };
    return;
  }

  const courseInfo = JSON.parse(
    JSON.stringify(await query(COURSEINFO_SQL, [course_id]))
  );
  let item = courseInfo.data[0];
  if (item.spare_time == 0) {
    ctx.body = {
      status: 200,
      result: false,
      message: "课程余量不足",
    };
    return;
  }
  item.spare_time--;
  const course = await query(COURSESPARETIME_SQL, [item.spare_time, course_id]);
  const res = await query(ELECTIVEADD_SQL, queryData);
  if (res.result && course.result) {
    ctx.body = {
      status: 200,
      result: true,
      message: "选课成功",
    };
  } else {
    ctx.body = {
      status: 200,
      result: false,
      message: "选课失败",
    };
  }
});

/**
 * 修改成绩
 * @url /elective/:user_id/:course_id/info
 */
router.put(`/:user_id/:course_id/info`, async (ctx) => {
  const request = ctx.request.body;
  const { user_id, course_id } = ctx.params;
  const { grade } = request;
  const queryData = [grade, user_id, course_id];
  const res = await query(ELECTIVEUPDATE_SQL, queryData);
  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      message: "修改成功",
    };
  } else {
    ctx.body = {
      status: 200,
      result: false,
      message: "修改失败",
    };
  }
});

/**
 * 删除选课
 * @url /elective/:user_id/:course_id/info
 */
router.delete(`/:user_id/:course_id/info`, async (ctx) => {
  const { user_id, course_id } = ctx.params;
  if (course_id && user_id) {
    const res = await query(ELECTIVEDELETE_SQL, [user_id, course_id]);
    if (res.result) {
      ctx.body = {
        status: 200,
        result: true,
        message: "删除成功",
      };
    } else {
      ctx.body = {
        status: 200,
        result: false,
        message: "删除失败",
      };
    }
  }
});

module.exports = router;
